
#coding:utf-8

import redis
import json
import time

__redis_host = "smeug.nju.edu.cn"
__redis_port = 6379
__redis_db = 0

__rc_pool = redis.StrictRedis(host= __redis_host, port=__redis_port, db=__redis_db)

def pub(channel, message):
     __rc_pool.publish(channel, message)


def sub(channel):
    sub = __rc_pool.pubsub()
    sub.subscribe(channel)
    return sub

#一个用于处理超时自动pub消息的函数
def expired_pub(channel):
    message = {
        "err_code":-1,
        "description":"expried!"
    }
    def inner_func():
        time.sleep(20)
        __rc_pool.publish(channel, json.dumps(message))
    return inner_func